#include "SkyBox.h"
#include "Creator.h"

hxe::SkyBox* hxe::SkyBox::skybox = nullptr;

hxe::SkyBox::SkyBox(Material* material)
{
    this->material = material;
    this->boxMesh = Creator::createCubeMesh(1.0f, 1.0f, 1.0f);
}

hxe::SkyBox::~SkyBox()
{
    material->drop();
    boxMesh->drop();
}

void hxe::SkyBox::draw()
{
    glDepthFunc(GL_LEQUAL);
    material->prepareDraw();

    Camera* camera = Camera::getMainCamera();
    Matrix4x4 view = Matrix4x4(Matrix3x3(camera->getViewMatrix()));
    Matrix4x4 viewProjTrans = camera->getProjectionMatrix() * view;
    Shader* shader = material->getShader();
    shader->setMatrix("viewProjTrans", viewProjTrans);

    glBindVertexArray(boxMesh->vao);
    glDrawElements(GL_TRIANGLES, (int)(boxMesh->indices->size()), GL_UNSIGNED_INT, 0);

    glDepthFunc(GL_LESS);
}
